package com.haiermzj.client.activity;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.haiermzj.client.R;
import com.haiermzj.client.utils.Contance;

public class TakePicActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView openXc;
    private ImageView takePic;
    private int OPEN_XC = 1000;
    private ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_pic);
        initView();
    }

    private void initView() {
        openXc = (ImageView) findViewById(R.id.open_photograph_album);
        takePic = (ImageView) findViewById(R.id.take_pic);
        image = (ImageView) findViewById(R.id.image);
        openXc.setOnClickListener(this);
        takePic.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.open_photograph_album:
                /*打开相册*/
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, OPEN_XC);
                break;

            case R.id.take_pic:

                if (Contance.connect != null) {
                    //给镜子下发指令，拍照
                    Contance.connect.send(Contance.TAKE_PIC.getBytes());
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == OPEN_XC && resultCode == RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            String[] filePathColumns = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePathColumns[0]);
            String imagePath = c.getString(columnIndex);
            image.setImageBitmap(BitmapFactory.decodeFile(imagePath));
            Log.e("====", "onActivityResult: " + imagePath);
        }


    }
}
